package com.example.practice;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by IntelliJ IDEA2021.3
 *
 * @Author: Tenghw
 * @Date: 2022/10/08  12:49
 * @Description: CAS:比较并交换
 */
public class CASdemo {
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger();
        System.out.println(atomicInteger);
        //compareAndSet返回的是boolean类型，修改成功返回true，失败返回false
        System.out.println(atomicInteger.compareAndSet(0, 666) + "\t current data is " + atomicInteger.get());

        System.out.println(atomicInteger.compareAndSet(1, 777) + "\t current data is " + atomicInteger.get());

        System.out.println(atomicInteger.compareAndSet(666, 888) + "\t current data is " + atomicInteger.get());

        //System.out.println(atomicInteger.getAndIncrement());
        System.out.println(atomicInteger.get());
    }
}
